package com.bitejiuyeke.forum.services.impl;

import com.bitejiuyeke.forum.common.AppResult;
import com.bitejiuyeke.forum.common.ResultCode;
import com.bitejiuyeke.forum.dao.ArticleMapper;
import com.bitejiuyeke.forum.exception.ApplicationException;
import com.bitejiuyeke.forum.model.Article;
import com.bitejiuyeke.forum.services.IArticleService;
import com.bitejiuyeke.forum.services.IBoardService;
import com.bitejiuyeke.forum.services.IUserService;
import com.bitejiuyeke.forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author 比特就业课
 */
@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private IUserService userService;
    @Resource
    private IBoardService boardService;

    @Override
    public List<Article> selectAll() {
        // 调用DAO
        List<Article> result = articleMapper.selectAll();
        // 返回结果
        return result;
    }

    @Override
    public List<Article> selectByBoardId(Long boardId) {
        // 非空校验
        if (boardId == null || boardId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAO
        List<Article> result = articleMapper.selectByBoardId(boardId);
        // 返回结果
        return result;
    }

    @Override
    public void create(Article article) {
        // 非空校验
        if (article == null || article.getBoardId() == null || article.getBoardId() <= 0
            || article.getUserId() == null || article.getUserId() <= 0
            || StringUtils.isEmpty(article.getTitle())
            || StringUtils.isEmpty(article.getContent())) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 设置默认值
        article.setVisitCount(0); // 访问数量
        article.setReplyCount(0); // 回复数量
        article.setLikeCount(0); // 点赞数量
        article.setState((byte) 0); // 状态
        article.setDeleteState((byte) 0); // 是否删除
        Date date = new Date();
        article.setCreateTime(date); // 创建时间
        article.setUpdateTime(date); // 更新时间
        // 调用DAO
        int articleRow = articleMapper.insertSelective(article);
        if (articleRow != 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_CREATE.toString() + " 新增帖子失败");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

        // 更新用户的帖子数
        userService.addOneArticleCountById(article.getUserId());
        // 更新版块的帖子数
        boardService.addOneArticleCountById(article.getBoardId());
        // 打印日志
        log.info(ResultCode.SUCCESS.toString() + " 帖子发布成功, articleId = " + article.getId());

    }

    @Override
    public Article selectById(Long id) {
        // 非空校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAO
        Article article = articleMapper.selectById(id);
        // 返回结果
        return article;
    }

    @Override
    public List<Article> selectByUserId(Long userId) {
        // 非空校验
        if (userId == null || userId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAO
        List<Article> result = articleMapper.selectByUserId(userId);
        // 返回结果
        return result;
    }

    @Override
    public void updateVisitCountById(Long id) {
        // 非空校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAO
        int row = articleMapper.updateVisitCountById(id);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString() + " 更新访问次数失败");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public void modify(Long id, String title, String content) {
        // 非空校验
        if (id == null || id <= 0 || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 构造更新对象
        Article article = new Article();
        article.setId(id); // 帖子Id
        article.setTitle(title); // 标题
        article.setContent(content); // 内容
        // 更新时间
        Date date = new Date();
        article.setUpdateTime(date);

        // 调用DAO
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString() + " 修改帖子失败");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public void updateById(Article article) {
        if (article == null || article.getId() == null || article.getId() <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 更新时间，保证更新操作永远都是正常的
        // 如果需要抛出更新异常，可以把这行注掉
//        article.setUpdateTime(new Date());
        // 调用DAO
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }
}
